import {defineStore} from "pinia";
import {ElMessage} from "element-plus";
import {
    updateInformationRecord,
    addCategories,
    delCategories,
    findAllCategories,
    getCategories,
    updateCategories,
    findAllMyCategories
} from "@/api/categories";

export const CategoriesStore = defineStore('categories', {
    state:()=>{
        return {
            // 列表
            categoriesList: [],
            // 详细信息
            categoriesInfo: {}
        }
    },
    actions:{
        updateInformationRecord(id,type){
            return new Promise((resolve, reject)=>{
                updateInformationRecord(id,{type}).then(res => {
                    resolve(res.code)
                    this.getInformationInfo(id)
                }).catch(err => {
                    reject()
                })
            })
        },
        addCategories(){
            return new Promise((resolve, reject)=>{
                addCategories(this.categoriesInfo).then(res => {
                    resolve(res.code)
                    this.categoriesInfo = {}
                }).catch(err => {
                    reject()
                })
            })
        },
        async getMyCategoriesList(query) {
            return new Promise((resolve, reject)=>{
                findAllMyCategories({...query}).then(res => {
                    this.categoriesList = res.result.list
                    resolve(res.result)
                }).catch(err => {
                    reject()
                })
            })
        },
        async getCategoriesList(query) {
            return new Promise((resolve, reject)=>{
                findAllCategories({...query}).then(res => {
                    this.categoriesList = res.result.list
                    resolve(res.result)
                }).catch(err => {
                    reject()
                })
            })
        },
        async getCategoriesInfo(id) {
            const {result} = await getCategories(id)
            this.categoriesInfo = result
            this.categoriesInfo.categories_price = parseFloat(this.categoriesInfo.categories_price)
        },
        updateCategories(id,data) {
            return new Promise(async (resolve, reject) => {
                updateCategories(id,data).then(res => {
                    resolve(res)
                }).catch(err => {
                    reject(err)
                })
            })
        },
        async deleteCategories(id) {
            const {code} = await delCategories(id)
        },
    }
})